package com.chery.media.library.extend

import android.graphics.Color
import androidx.annotation.*
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.Utils

/**
 * @author MoYe_PanDeng
 * @ClassName:
 * @Description:
 * @date: 4/7/21
 */
/**
 * 根据 id 获取颜色
 */
fun @receiver:ColorRes Int.toColorInt(): Int {
    return try {
        //Utils.getApp().resources.getColor(this)
//        ContextCompat.getColor(Utils.getApp(), this)
        ColorUtils.getColor(this)
    } catch (e: Exception) {
        e.printStackTrace()
        Color.WHITE
    }
}

/**
 * 根据 id 获取图片
 */
fun @receiver:DrawableRes Int.toDrawable() = try {
    //Utils.getApp().resources.getDrawable(this)
    val drawable = ContextCompat.getDrawable(Utils.getApp(), this)
    drawable?.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
    drawable
} catch (e: Exception) {
    e.printStackTrace()
    null
}

/**
 * 根据 id 获取字符串
 */
fun @receiver:StringRes Int.toResString(vararg formatArgs: Any) = try {
    Utils.getApp().resources.getString(this, *formatArgs)
} catch (e: Exception) {
    e.printStackTrace()
    Utils.getApp().resources.getString(this)
}

/**
 * 根据 id 获取字符串数组
 */
fun @receiver:ArrayRes Int.toResStringArray() = try {
    Utils.getApp().resources.getStringArray(this)
} catch (e: Exception) {
    e.printStackTrace()
}

/**
 * 根据 id 获取Dimension
 */
fun @receiver:DimenRes Int.toResDimension() = try {
    Utils.getApp().resources.getDimension(this)
} catch (e: Exception) {
    0f
}